﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Store
{
    public partial class frmSamplePreOpetation : Form
    {
        Entities db = new Entities();

        public List<T_Sample> TSample = null;

        T_Sample m_Sample = null;

        T_Flow m_Flow = null;

        private bool isEnd = false;
        public List<int> xID
        {
            get;
            set;
        }
        public frmSamplePreOpetation()
        {
            InitializeComponent();

            Service.CommandName(cbCommandName);
        }

        private void SetColumn()
        {
            int i=0;
            foreach (var list in TSample)
            {
                int? n=0;
                gv1.Rows[i].Cells["VpCount"].Value = list.pCount;
                gv1.Rows[i].Cells[10].Value = n;
                gv1.Rows[i].Cells[11].Value = n;
                gv1.Rows[i].Cells[12].Value = n;
                gv1.Rows[i].Cells[13].Value = n;
                i++;
            }
        }

        private void gv1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (isEnd)
            {
                if (gv1.Columns[e.ColumnIndex].Name.Equals("send") || gv1.Columns[e.ColumnIndex].Name.Equals("store") || gv1.Columns[e.ColumnIndex].Name.Equals("sr") || gv1.Columns[e.ColumnIndex].Name.Equals("oout"))
                {
                    try
                    {
                        if (e.RowIndex < 0) return;
                        DataGridViewCell cell = gv1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        if (cell != null)
                        {
                            if (Convert.ToInt32(cell.Value) > Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["pCount"].Value))
                            {
                                Msg.ShowWarning(gv1.Columns[cell.ColumnIndex].HeaderText + "大于预备区数量");
                                
                                gv1.Rows[e.RowIndex].Cells["VpCount"].Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["pCount"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["send"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["store"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["sr"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["oout"].Value)+Convert.ToInt32(cell.Value);
                                cell.Value = 0;
                                return;
                            }
                            else
                            {
                                gv1.Rows[e.RowIndex].Cells["VpCount"].Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["pCount"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["send"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["store"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["sr"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["oout"].Value);
                              
                            }

                            if (cell.ColumnIndex == 12)
                            {
                                if (Convert.ToInt32(this.gv1.Rows[e.RowIndex].Cells["srCount"].Value.ToString()) == 1)
                                {
                                    Msg.ShowWarning("样品间已有该品牌的记录，不能重复放入");
                                    this.gv1.Rows[e.RowIndex].Cells["sr"].Value = 0;
                                    return;
                                }
                                if (Convert.ToInt32(cell.Value) > 1)
                                {
                                    Msg.ShowWarning("样品间数量只能填写1");
                                    this.gv1.Rows[e.RowIndex].Cells["sr"].Value = 1;
                                    return;
                                }
                            }

                            if (Convert.ToInt32(this.gv1.Rows[e.RowIndex].Cells["send"].Value.ToString()) +
                                Convert.ToInt32(this.gv1.Rows[e.RowIndex].Cells["store"].Value.ToString()) +
                                Convert.ToInt32(this.gv1.Rows[e.RowIndex].Cells["sr"].Value.ToString()) +
                                Convert.ToInt32(this.gv1.Rows[e.RowIndex].Cells["oout"].Value.ToString())
                                > Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["pCount"].Value))
                            {
                                Msg.ShowWarning("操作数量大于预备区数量");
                                gv1.Rows[e.RowIndex].Cells["VpCount"].Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["pCount"].Value);
                                this.gv1.Rows[e.RowIndex].Cells["send"].Value = 0;
                                this.gv1.Rows[e.RowIndex].Cells["store"].Value = 0;
                                this.gv1.Rows[e.RowIndex].Cells["oout"].Value = 0;
                                this.gv1.Rows[e.RowIndex].Cells["sr"].Value = 0;
                                return;
                            }
                        }
                    }
                    catch (Exception a)
                    {
                        Msg.ShowWarning(a.Message);
                    }
                }
            }
        }

        private void btnOperation_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbCommandName.Text))
            {
                Msg.ShowInfo("请选择指令人");
                return;
            }
            try
            {
                if (this.gv1.EndEdit())
                {
                    for (int i = 0; i < gv1.RowCount; i++)
                    {
                        if (gv1.Rows[i].Cells["send"].Value.ToString() == "0" && gv1.Rows[i].Cells["store"].Value.ToString() == "0" && gv1.Rows[i].Cells["sr"].Value.ToString() == "0" && gv1.Rows[i].Cells["oout"].Value.ToString() == "0")
                        {
                            Msg.ShowWarning("请设置" + gv1.Rows[i].Cells["colProductNo"].Value.ToString() + "的操作数量！");
                            return;
                        }
                    }
                    int z = 0;
                    foreach(var list in TSample)
                    {
                        m_Sample = list;

                        if (Convert.ToInt32(gv1.Rows[z].Cells["store"].Value.ToString()) > 0)
                        {
                            if (string.IsNullOrEmpty(txtlocation.Text))
                            {
                                Msg.ShowWarning("请选择仓位！");
                                return;
                            }
                            else
                            {
                                if (db.T_Store.Where(a => a.location == txtlocation.Text).Count() <= 0)
                                {
                                    Msg.ShowWarning("无此仓位,请重新输入!");
                                    return;
                                }
                                else
                                {
                                    m_Sample.location = txtlocation.Text;
                                    m_Sample.sCount += Convert.ToInt32(gv1.Rows[z].Cells["store"].Value.ToString());

                                    m_Flow = new T_Flow();
                                    if (db.T_Flow.Count() <= 0) m_Flow.cID = 1;
                                    else m_Flow.cID = db.T_Flow.OrderByDescending(a => a.cID).First().cID + 1;
                                    m_Flow.xID = m_Sample.xID;
                                    m_Flow.sCount = Convert.ToInt32(gv1.Rows[z].Cells["store"].Value.ToString());
                                    m_Flow.reason = "入仓(预备区)";
                                    m_Flow.opName = Startup.GetApp().CurrentUser.name;
                                    m_Flow.opDate = DateTime.Now.ToLocalTime();
                                    m_Flow.commandName = cbCommandName.Text;
                                    db.T_Flow.Add(m_Flow);
                                    db.SaveChanges();
                                }
                            }
                        }
                        if (Convert.ToInt32(gv1.Rows[z].Cells["send"].Value.ToString()) > 0)
                        {
                            m_Sample.jCount+=Convert.ToInt32(gv1.Rows[z].Cells["send"].Value.ToString());

                            m_Flow = new T_Flow();
                            if (db.T_Flow.Count() <= 0) m_Flow.cID = 1;
                            else m_Flow.cID = db.T_Flow.OrderByDescending(a => a.cID).First().cID + 1;
                            m_Flow.xID = m_Sample.xID;
                            m_Flow.sCount = Convert.ToInt32(gv1.Rows[z].Cells["send"].Value.ToString());
                            m_Flow.reason = "寄出";
                            m_Flow.opName = Startup.GetApp().CurrentUser.name;
                            m_Flow.opDate = DateTime.Now.ToLocalTime();
                            m_Flow.commandName = cbCommandName.Text;
                            db.T_Flow.Add(m_Flow);
                            db.SaveChanges();
                        }
                        if (Convert.ToInt32(gv1.Rows[z].Cells["sr"].Value.ToString()) > 0)
                        {
                            m_Sample.srCount = Convert.ToInt32(gv1.Rows[z].Cells["sr"].Value.ToString());

                            m_Flow = new T_Flow();
                            if (db.T_Flow.Count() <= 0) m_Flow.cID = 1;
                            else m_Flow.cID = db.T_Flow.OrderByDescending(a => a.cID).First().cID + 1;
                            m_Flow.xID = m_Sample.xID;
                            m_Flow.sCount = Convert.ToInt32(gv1.Rows[z].Cells["sr"].Value.ToString());
                            m_Flow.reason = "样品间(预备区)";
                            m_Flow.opName = Startup.GetApp().CurrentUser.name;
                            m_Flow.opDate = DateTime.Now.ToLocalTime();
                            m_Flow.commandName = cbCommandName.Text;
                            db.T_Flow.Add(m_Flow);
                            db.SaveChanges();
                        }
                        if (Convert.ToInt32(gv1.Rows[z].Cells["oout"].Value.ToString())>0)
                        {
                            m_Sample.oCount += Convert.ToInt32(gv1.Rows[z].Cells["oout"].Value.ToString());

                            m_Flow = new T_Flow();
                            if (db.T_Flow.Count() <= 0) m_Flow.cID = 1;
                            else m_Flow.cID = db.T_Flow.OrderByDescending(a => a.cID).First().cID + 1;
                            m_Flow.xID = m_Sample.xID;
                            m_Flow.sCount = Convert.ToInt32(gv1.Rows[z].Cells["oout"].Value.ToString());
                            m_Flow.reason = "出仓(预备区)";
                            m_Flow.opName = Startup.GetApp().CurrentUser.name;
                            m_Flow.opDate = DateTime.Now.ToLocalTime();
                            m_Flow.commandName = cbCommandName.Text;
                            db.T_Flow.Add(m_Flow);
                            db.SaveChanges();
                        }
                        m_Sample.pCount -= (Convert.ToInt32(gv1.Rows[z].Cells["send"].Value.ToString())+ Convert.ToInt32(gv1.Rows[z].Cells["store"].Value.ToString()) + Convert.ToInt32(gv1.Rows[z].Cells["sr"].Value.ToString()) + Convert.ToInt32(gv1.Rows[z].Cells["oout"].Value.ToString()));

                        db.SaveChanges();
                    }

                    Msg.ShowInfo("完成预备区操作！"); 

                    
                    frmSampleSend send = new frmSampleSend();
                    int[] xID = new int[gv1.RowCount];
                    int[] jCount = new int[gv1.RowCount];
                    for (int i=0;i<gv1.RowCount;i++)
                    {
                        if (Convert.ToInt32(gv1.Rows[i].Cells["send"].Value.ToString()) > 0)
                        {
                            xID[i] = int.Parse(gv1.Rows[i].Cells["colxID"].Value.ToString());
                            jCount[i] = Convert.ToInt32(gv1.Rows[i].Cells["send"].Value.ToString());
                        }
                    }

                    if (xID[0] !=0)
                    {
                        send.xID = xID;
                        send.jCount = jCount;
                        send.ShowPanDetail();
                        send.Show();
                        send.Activate();
                    }

                    DialogResult = System.Windows.Forms.DialogResult.OK;
                    
                    this.Close();

                }
                else
                {
                    Msg.ShowWarning("请结束编辑！");
                }
            }
            catch(Exception x)
            {
                Msg.ShowError(x.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmSamplePreOpetation_Load(object sender, EventArgs e)
        {
            if (xID.Count <= 0) return;

            TSample = new List<T_Sample>();
            for (int i = 0; i < xID.Count;i++ )
            {
                var id = xID[i];
                var list = db.T_Sample.Where(a => a.xID == id);
               
                TSample.Add(list.FirstOrDefault()); 
            }
            bs.DataSource = TSample;

            SetColumn();

            isEnd = true;
            Service.CommandName(cbCommandName);
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            Excel.ExcelSave(gv1);
        }
    }
}
